package game;

public class Game {
	public static void main(String[] args) {
		
			playGame();
	}
	
	public static void playGame()
	{
		Cup cup = new Cup();
		Gameboard board = new Gameboard();
		GUIout out = new GUIout();
		GUIin in = new GUIin();
		
		while (true) {
		
		out.startGUI();
		out.welcomeText();
		int playerfield;
		int activeplayers = in.getNumberOfPlayers();
		Player[] players = new Player[activeplayers];
		players = in.addPlayers(players);
		
		int i=0;
		while (true)
		{
			if (players[i].isActive()) {
				out.turnText(players[i].getName());
				cup.rollDices();
				out.setDices(cup.getArray());
				out.removeCar(players[i].getField(), players[i].getName());
				players[i].move(cup.getRoll());
				playerfield=players[i].getField();
				out.setCar(playerfield, players[i].getName());
				switch (board.getField(playerfield).getFieldValue())
				{
				case 1:		//Territory. 
					if (board.getField(playerfield).isOwnable()) {
						if (in.yesOrNo()) {
							players[i].setChoice(1);
							board.getField(playerfield).landOnField(players[i]);
							out.setBalance(players[i].getName(), players[i].getAccount());
						}
					} 
					else { 	//Hvis den er ejet, betales rent.
						if (!(board.getField(playerfield).isOwner(players[i])))
						{
							out.landOnElsesField();
							board.getField(playerfield).landOnField(players[i]);
							out.setBalance(players[i].getName(), players[i].getAccount());
						}
						else {
							out.landAsOwner();
						}
					}
					break;
				case 2: 	//LaborCamp.
					if (board.getField(playerfield).isOwnable()) {
						if (in.yesOrNo()) {
							players[i].setChoice(1);
							board.getField(playerfield).landOnField(players[i]);
							out.setBalance(players[i].getName(), players[i].getAccount());
						}
					} 
					else {
						if (!(board.getField(playerfield).isOwner(players[i])))
						{
						out.landOnLabor(board.getField(playerfield).getName());
						cup.rollDices();
						players[i].setChoice(cup.getRoll());
						out.setDices(cup.getArray());
						board.getField(playerfield).landOnField(players[i]);
						out.setBalance(players[i].getName(), players[i].getAccount());
						} else {
							out.landAsOwner();
						}
						
					}
					break;
				case 3: //Fleet.
					if (board.getField(playerfield).isOwnable()) {
						if (in.yesOrNo()) {
							players[i].setChoice(1);
							board.getField(playerfield).landOnField(players[i]);
							out.setBalance(players[i].getName(), players[i].getAccount());
						}
					} 
					else {
						if (!(board.getField(playerfield).isOwner(players[i])))
						{
							out.landOnField(board.getField(playerfield).getName());
							out.landOnElsesField();
							board.getField(playerfield).landOnField(players[i]);
							out.setBalance(players[i].getName(), players[i].getAccount());
						} else {
							out.landAsOwner();
						}
					}
					break;
				case 4: //Der er to forskellige typer af Tax.
					if (board.getField(playerfield).getNumber()==16)
					{ //Hvis feltnummeret er 16 betales der bare fastbeløb.
						out.goldMine();
						players[i].setChoice(1);
						board.getField(playerfield).landOnField(players[i]);
						out.setBalance(players[i].getName(), players[i].getAccount());
					} else if (board.getField(playerfield).getNumber()==17)
					{ //Hvis feltnummeret er 17 skal der vælges.
						if (in.landOnTax(players[i].getAccount())) {
							players[i].setChoice(1);
							board.getField(playerfield).landOnField(players[i]);
							out.setBalance(players[i].getName(), players[i].getAccount());
						} else {
							players[i].setChoice(2);
							board.getField(playerfield).landOnField(players[i]);
							out.setBalance(players[i].getName(), players[i].getAccount());
						}
					}
					break;
				case 5:
					out.landOnRefuge();
					board.getField(playerfield).landOnField(players[i]);
					out.setBalance(players[i].getName(), players[i].getAccount());
					break;
				}
			}
			if (players[i].hasLost() && players[i].isActive())
			{
				out.playerHasLost(players[i].getName(), players[i].getField());
				players[i].bankrupt();
				activeplayers--;
			}
		
			i++;
			if (i==players.length) i=0;
			
			if (activeplayers==1) break;
		}
		
		for (i=0; i<players.length; i++)
		{
			if (players[i].isActive()) break;
		}
		out.playerHasWon(players[i].getName());
		out.closeGUI();
		if (!in.playAgain()) break;
		}
	}
}